<?php

/**
 * 项目SQL发布管理 ajax类
 * @package  ajax
 * @subpackage  backend
 * @create by allen.qiang
 */
header('Content-Type:text/html;charset=utf-8');
require 'quanliadmin/app/project/action/sqlHandler.class.php';

class ajax_projectSql {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //发布的SQL列表
                    $this->sqlList();
                    break;

                case 2: //发布一条SQL
                    $this->sqlAdd();
                    break;

                case 3: //获取某个项目所有的数据库
                    $this->getProjectDataBaseList();
                    break;

                case 4: //在单个数据库中执行SQL
                    $this->executeSql();
                    break;

            }

        }

    }

    //发布SQL列表
    public function sqlList(){

        global $G_PGLOB;

        //查询发布的SQL列表信息
        $sqlHandler = new sqlHandler( $G_PGLOB->DBCA ) ;
        $result_data	  = $sqlHandler->getSqlList( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        foreach($result_data as $data) {

            $result_array['rows'][] = array(
                'id' 	=> $data['seqno'],
                'cell' 	=> array(
                    '<a href="javascript: void(0);" onclick="sqlProject.detail(' . $data['seqno'] . ')">详情</a>&nbsp;&nbsp;'.
                    '<a href="javascript: void(0);" onclick="sqlProject.look(' . $data['seqno'] . ')">查看SQL</a>',
                    $data['creator'],
                    $data['seqno'],
                    $data['createTime'],
                    $data['projectName'],
                    $data['memo']
                )
            );
        }

        echo json_encode( $result_array );

    }

    /**
     * 更新或保存发布的SQL
     */
    private function sqlAdd() {

        global $G_PGLOB;

        //实例化SQL发布管理类
        $sqlHandler = new sqlHandler( $G_PGLOB->DBCA ) ;

        //保存发布的信息并执行SQL
        $result_array = $sqlHandler->issueSql( $_REQUEST ) ;
        echo json_encode( $result_array );
        return;

    }

    //获取项目下所有的数据库
     function getProjectDataBaseList(){

         global $G_PGLOB;

         //实例化SQL发布管理类
         $sqlHandler = new sqlHandler( $G_PGLOB->DBCA );

         //获取该项目中的数据库
         $returnArr = $sqlHandler->getProjectDataBase( $_REQUEST );

         echo json_encode( array( 'code'=>200 , 'data'=>$returnArr ) );
         return ;

     }

    //单个执行SQL
    function executeSql(){

        global $G_PGLOB;

        //实例化SQL发布管理类
        $sqlHandler = new sqlHandler( $G_PGLOB->DBCA );

        //在单个数据库中执行SQL
        $returnArr = $sqlHandler->executeSql( $_REQUEST );

        echo json_encode( $returnArr );
        return;

    }


}